package com.xian.cloud.controller;

import com.xian.cloud.entity.UserEntity;
import com.xian.cloud.face.UserService;
import com.xian.cloud.util.TestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author: xlr
 * @Date: Created in 下午10:35 2018/10/28
 */
@RestController
@RequestMapping("lcn")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    Environment env;

    @RequestMapping(value = "create",method = RequestMethod.POST)
    public String create(@RequestBody UserEntity user){
        boolean flag = userService.insert( user );
        if(flag){
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping(value = "info/{id}",method = RequestMethod.GET)
    public UserEntity info(@PathVariable("id") Long id){

        UserEntity user = userService.selectById( id );
        if (user != null) {
            return user;
        }else {
            return null;
        }
    }

    @RequestMapping("start")
    public void startTime(){
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("task  run:"+ new Date());
            }
        };

        Timer timer = new Timer();
        //安排指定的任务在指定的时间开始进行重复的固定延迟执行。这里是每3秒执行一次
        timer.schedule(timerTask,10,30000);

    }

    @RequestMapping("test")
    public void testSourse(){
        System.out.println(env.getProperty( "abc" ));
    }

    @RequestMapping("abc")
    public String test(){
        System.out.println(TestUtil.getTest());
        return env.getProperty( "abc" );
    }

}
